<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主表id" prop="formId">
        <el-select
          v-model="queryParams.formId"
          placeholder="请选择调查问卷"
          clearable
          filterable
          @change="handleQuery"
        >
          <el-option
            v-for="item in queFormList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['secondVitim:QueFormDetail:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['secondVitim:QueFormDetail:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['secondVitim:QueFormDetail:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['secondVitim:QueFormDetail:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="QueFormDetailList" @selection-change="handleSelectionChange" :expand-row-keys="expandedRows" row-key="id">
      <el-table-column type="expand" width="30">
        <template slot-scope="props">
          <div v-if="props.row.queFormDetailOptionList && props.row.queFormDetailOptionList.length > 0" style="padding: 10px;">
            <h4>选项详情：</h4>
            <el-table :data="props.row.queFormDetailOptionList" size="mini" border>
              <el-table-column label="选项键值" prop="optionValue" align="center" />
              <el-table-column label="选项文本" prop="optionText" align="center" />
              <el-table-column label="排序" prop="sort" align="center" />
              <el-table-column label="状态" prop="status" align="center">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.status"/>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div v-else style="padding: 20px; text-align: center; color: #999;">
            暂无选项详情
          </div>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="问卷名称" align="center" prop="formName" />
      <el-table-column label="问题" align="center" prop="title" />
      <el-table-column label="问题类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.que_form_detail_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="是否必填" align="center" prop="required">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.question_is_required" :value="scope.row.required"/>
        </template>
      </el-table-column>
      <el-table-column label="答案" align="center" prop="remark" />
      <el-table-column label="排序" align="center" prop="orderNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['secondVitim:QueFormDetail:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['secondVitim:QueFormDetail:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改问卷细项对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="问卷名称" prop="formName">
          <el-select :disabled="form.id"
            v-model="queryParams.formId"
            placeholder="请选择调查问卷"
            clearable
            filterable
          >
            <el-option
              v-for="item in queFormList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="问题" prop="title">
          <el-input v-model="form.title" placeholder="请输入问题" />
        </el-form-item>
        <el-form-item label="类型" prop="required">
          <el-select v-model="form.type" placeholder="请选择">
            <el-option v-for="dict in dict.type.que_form_detail_type" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否必填" prop="required">
          <el-select v-model="form.required" placeholder="是否必填">
            <el-option v-for="dict in dict.type.question_is_required" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="答案" prop="remark">
          <el-input v-model="form.remark" placeholder="答案" />
        </el-form-item>
        <el-divider content-position="center">调查问卷详情选项信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddQueFormDetailOption">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteQueFormDetailOption">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="queFormDetailOptionList" :row-class-name="rowQueFormDetailOptionIndex" @selection-change="handleQueFormDetailOptionSelectionChange" ref="queFormDetailOption">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="选项键值" prop="optionValue" width="100">
            <template slot-scope="scope">
              <el-input v-model="scope.row.optionValue" placeholder="请输入选项键值" />
            </template>
          </el-table-column>
          <el-table-column label="选项键值对应文本" prop="optionText" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.optionText" placeholder="请输入选项键值对应文本" />
            </template>
          </el-table-column>
          <el-table-column label="排序字段" prop="sort" width="80">
            <template slot-scope="scope">
              <el-input v-model="scope.row.sort" placeholder="" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQueFormDetail, getQueFormDetail, delQueFormDetail, addQueFormDetail, updateQueFormDetail } from "@/api/secondVitim/QueFormDetail"
import { listQueForm } from "@/api/secondVitim/queForm"

export default {
  name: "QueFormDetail",
  dicts: ['que_form_detail_type', 'question_is_required'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedQueFormDetailOption: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问卷细项表格数据
      QueFormDetailList: [],
      // 主表调查问卷列表数据
      queFormList: [],
      // 调查问卷详情选项表格数据
      queFormDetailOptionList: [],
      // 展开的行
      expandedRows: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        formId: null,
        type: null,
        title: null,
        orderNum: null,
        options: null,
        status: null,
        required: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
    this.getQueFormList()
  },
  methods: {
    /** 查询问卷细项列表 */
    getList() {
      this.loading = true
      listQueFormDetail(this.queryParams).then(response => {
        this.QueFormDetailList = response.rows
        this.total = response.total
        this.loading = false
        // 清空展开的行
        this.expandedRows = []
      })
    },
    /** 查询主表调查问卷列表 */
    getQueFormList() {
      const query = {
        status: 0
      }
      listQueForm(query).then(response => {
        this.queFormList = response.rows || []
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        formId: null,
        type: null,
        title: null,
        orderNum: null,
        options: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        status: null,
        required: null,
        remark: null
      }
      this.queFormDetailOptionList = []
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加问卷细项"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getQueFormDetail(id).then(response => {
        this.form = response.data
        this.queFormDetailOptionList = response.data.queFormDetailOptionList
        this.open = true
        this.title = "修改问卷细项"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.queFormDetailOptionList = this.queFormDetailOptionList
          if (this.form.id != null) {
            updateQueFormDetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            this.form.formId = this.queryParams.formId;
            addQueFormDetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = falsesss
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除问卷细项编号为"' + ids + '"的数据项？').then(function() {
        return delQueFormDetail(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
	/** 调查问卷详情选项序号 */
    rowQueFormDetailOptionIndex({ row, rowIndex }) {
      row.index = rowIndex + 1
    },
    /** 调查问卷详情选项添加按钮操作 */
    handleAddQueFormDetailOption() {
      let obj = {}
      obj.optionValue = ""
      obj.optionText = ""
      obj.remarks = ""
      obj.status = ""
      obj.sort = ""
      this.queFormDetailOptionList.push(obj)
    },
    /** 调查问卷详情选项删除按钮操作 */
    handleDeleteQueFormDetailOption() {
      if (this.checkedQueFormDetailOption.length == 0) {
        this.$modal.msgError("请先选择要删除的调查问卷详情选项数据")
      } else {
        const queFormDetailOptionList = this.queFormDetailOptionList
        const checkedQueFormDetailOption = this.checkedQueFormDetailOption
        this.queFormDetailOptionList = queFormDetailOptionList.filter(function(item) {
          return checkedQueFormDetailOption.indexOf(item.index) == -1
        })
      }
    },
    /** 复选框选中数据 */
    handleQueFormDetailOptionSelectionChange(selection) {
      this.checkedQueFormDetailOption = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('secondVitim/QueFormDetail/export', {
        ...this.queryParams
      }, `QueFormDetail_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
